package com.scu.controller;


import com.scu.pojo.CollectionTopic;
import com.scu.pojo.Report;
import com.scu.pojo.Topic;
import com.scu.pojo.User;
import com.scu.service.TopicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.awt.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class TopicController {

    @Autowired
    private TopicService topicService;


    //测试用
    public Topic czzSetTopic(){
        Topic czzT = new Topic();

        return czzT;
    }

    //获取帖子列表 b_id或u_id
    @RequestMapping("getTopicList")
    @ResponseBody
    public Map getTopicList(HttpServletRequest request){
        HashMap<String,Object> map = new HashMap<>();
        String bid = request.getParameter("bid");
        System.out.println("getTopicList");
        Topic topic = new Topic();
        topic.setB_id(bid);
        List<Topic> topicList = topicService.getTopicList(topic);
        System.out.println("getTopicList Type");
        map.put("code",200);
        map.put("msg","");
        map.put("data",topicList);

        return map;
    }


    /**
     * 返回该贴以及其评论
     * @param request
     * @return
     */
    @RequestMapping("getTopicCommentList")
    @ResponseBody
    public Map getTopicCommentList(HttpServletRequest request){

        System.out.println("getTopic");
        HashMap<String,Object> resultMap = new HashMap<>();
        Topic topic = new Topic();
        String t_id = (String) request.getSession().getAttribute("t_id");
        System.out.println("getTopic t_id : " + t_id);
        topic.setT_id(t_id);
        List<Topic> topicList = topicService.getTopicCommentList(topic);
        System.out.println("list size : " + topicList.size());
        if(topicList.size() == 1){
            resultMap.put("code",200);
            resultMap.put("msg","");
            resultMap.put("data",topicList);
        }else{
            resultMap.put("code",-2);
            resultMap.put("msg","过多的结果");
        }

        return resultMap;
    }

    //分页获取帖子信息  b_id
    @RequestMapping("getTopicListByPage")
    @ResponseBody
    public Map getTopicListByPage(Topic topic,int page, int limit){
        HashMap<String,Object> map = new HashMap<>();

        System.out.println("getTopicListByPage");

        List<Topic> topicList = topicService.getTopicListByPage(topic,page,limit);
        map.put("code",0);
        map.put("msg","");
        map.put("data",topicList);

        return map;
    }

    //分页获取精华帖  b_id
    @RequestMapping("getEliteTopicListByPage")
    @ResponseBody
    public Map getEliteTopicListByPage(Topic topic,int page, int limit){
        HashMap<String,Object> map = new HashMap<>();

        List<Topic> topicList = topicService.getEliteTopicListByPage(topic,page,limit);
        map.put("code",0);
        map.put("msg","");
        map.put("data",topicList);

        return map;
    }

    //获取我的帖子
    @RequestMapping("getMyTopicList")
    @ResponseBody
    public Map getMyTopicList(Topic topic, HttpSession session){
        HashMap<String,Object> map = new HashMap<>();
        // session 获取id
        topic.setT_author_id(session.getAttribute("u_id").toString());

        List<Topic> topicList = topicService.getMyTopicList(topic);
        System.out.println("topicPicture: " + topicList.size());
        map.put("code",0);
        map.put("msg","");
        map.put("data",topicList);

        return map;
    }

    //分页获取我的帖子
    @RequestMapping("getMyTopicListByPage")
    @ResponseBody
    public Map getMyTopicListByPage(Topic topic,int page, int limit, HttpSession session){
        HashMap<String,Object> map = new HashMap<>();

        // session 获取id
        topic.setT_author_id(session.getAttribute("u_id").toString());

        List<Topic> topicList = topicService.getMyTopicListByPage(topic,page,limit);
        map.put("code",0);
        map.put("msg","");
        map.put("data",topicList);

        return map;
    }

    //获取我的收藏
    @RequestMapping("getMyCollectionList")
    @ResponseBody
    public Map getMyCollectionTopicList(Topic topic, HttpSession session){
        System.out.println("collection");
        HashMap<String,Object> map = new HashMap<>();

        // session 获取id
        System.out.println("collection get uid");
        topic.setT_author_id(session.getAttribute("u_id").toString());
        System.out.println("collection set list");
        List<Topic> topicList = topicService.getMyCollectionTopicList(topic);
        System.out.println("collection Pic: " + topicList.size());

        map.put("code",0);
        map.put("msg","");
        map.put("data",topicList);

        return map;
    }

    //分页获取我的收藏
    @RequestMapping("getMyCollectionTopicListByPage")
    @ResponseBody
    public Map getMyCollectionTopicListByPage(Topic topic,int page, int limit, HttpSession session){
        HashMap<String,Object> map = new HashMap<>();

        // session 获取id
        topic.setT_author_id(session.getAttribute("u_id").toString());

        List<Topic> topicList = topicService.getMyCollectionTopicListByPage(topic,page,limit);
        map.put("code",0);
        map.put("msg","");
        map.put("data",topicList);

        return map;
    }



    //获取帖子回复数 t_id
    @RequestMapping("getCommentNum")
    @ResponseBody
    public Map getCommentNum(Topic topic){
        HashMap<String,Object> map = new HashMap<>();

        int commentNum = topicService.getCommentNum(topic);
        map.put("commentNum",commentNum);
        map.put("code",200);
        map.put("msg","");

        return map;
    }

    //发帖  t_title t_author_id t_type b_id
    @PostMapping("postTopic")
    @ResponseBody
    public Map postTopic(@RequestParam(value = "picture", required = false) MultipartFile picture, Topic topic, HttpSession session, HttpServletRequest request){
        HashMap<String,Object> map = new HashMap<>();
        System.out.println("postTopic");
        System.out.println(topic);
        String saveLocal = "";


        try {
            // 当前文件大小
            long currentFileSize = picture.getSize();
            // 上传源文件允许的最大值
            long fileLength = 5242880;
            String realPath = request.getSession().getServletContext().getRealPath("/");
            System.out.println("realPath " + realPath);
            if (currentFileSize <= fileLength) {
                System.out.println("文件类型：" + picture.getClass());
                System.out.println("文件：" + picture.getOriginalFilename());
                topicService.approvalFile(picture,realPath);
                String picturePath =  "/static/img/topicPicture/" + picture.getOriginalFilename();
                String titles = request.getParameter("titles");
                String fcontent = request.getParameter("fcontent");
                String bid = request.getParameter("bid");
                topic.setT_img(picturePath);
                topic.setT_content(fcontent);
                topic.setT_author_id(session.getAttribute("u_id").toString());
                topic.setT_elite_status("0");
                topic.setB_id(bid);
                topic.setT_title(titles);

                // 将article保存到数据库
                topicService.postTopic(topic);
                map.put("code",200);
                map.put("msg","文章上传成功");
                return map;
            } else {
                map.put("msg","上传文件过大");
                map.put("code",-1);
                return map;
            }
        } catch (Exception e) {
            e.printStackTrace();
            map.put("msg","发布文章失败");
            map.put("code",-2);
            return map;
        }
    }

    //修改帖子 t_title  t_id
    @RequestMapping("redactTopic")
    @ResponseBody
    public Map redactTopic(Topic topic){
        HashMap<String,Object> map = new HashMap<>();

        int result = topicService.redactTopic(topic);

        map.put("result",result);
        map.put("code",200);
        map.put("msg","");

        return map;
    }

    //获取返回发布文章数目最多的10名用户
    @RequestMapping("getPostMostUser")
    @ResponseBody
    public Map getPostMostUser(){
        HashMap<String,Object> map = new HashMap<>();

        List<User> userList = topicService.getPostMostUser();

        map.put("data",userList);
        map.put("code",200);
        map.put("msg","");

        return map;
    }


    //删除帖子 t_id  u_id
    @RequestMapping("removeTopicById")
    @ResponseBody
    public Map removeTopicById(HttpServletRequest request){
        HashMap<String,Object> map = new HashMap<>();
        String fid = request.getParameter("fid");
        Topic topic = new Topic();
        topic.setT_id(fid);
        List<Topic> topicList = topicService.getTopicByID(Integer.parseInt(fid));
        String imgDir = topicList.get(0).getT_img();
        String realPath = request.getSession().getServletContext().getRealPath("/");
        System.out.println("remove picture dir " + realPath + imgDir);
        topicService.deleteDir(realPath + imgDir);

        int result = topicService.removeTopicById(topic);
        map.put("code",200);
        map.put("msg","删除成功");
        map.put("result",result);
        return map;
    }

    //恢复删除帖子
    @RequestMapping("recoverTopicById")
    @ResponseBody
    public Map recoverTopicById(Topic topic){
        HashMap<String,Object> map = new HashMap<>();
        int result = topicService.recoverTopicById(topic);

        map.put("result",result);

        map.put("code",200);
        map.put("msg","");
        return map;
    }

    //置顶帖子
    @RequestMapping("setTopTopicById")
    @ResponseBody
    public Map setTopTopicById(Topic topic){
        HashMap<String,Object> map = new HashMap<>();

        int result = topicService.setTopTopicById(topic);

        map.put("result",result);

        map.put("code",200);
        map.put("msg","");
        return map;
    }

    //取消置顶
    @RequestMapping("removeTopTopicById")
    @ResponseBody
    public Map removeTopTopicById(Topic topic){
        HashMap<String,Object> map = new HashMap<>();

        int result = topicService.removeTopTopicById(topic);

        map.put("result",result);
        map.put("code",200);
        map.put("msg","");
        return map;
    }

    //设置精华帖
    @RequestMapping("setEliteTopicById")
    @ResponseBody
    public Map setEliteTopicById(Topic topic){
        HashMap<String,Object> map = new HashMap<>();

        int result = topicService.setEliteTopicById(topic);

        map.put("result",result);
        map.put("code",200);
        map.put("msg","");
        return map;
    }

    //取消精华贴
    @RequestMapping("removeEliteTopicById")
    @ResponseBody
    public Map removeEliteTopicById(Topic topic){
        HashMap<String,Object> map = new HashMap<>();

        int result = topicService.removeEliteTopicById(topic);

        map.put("result",result);

        map.put("code",200);
        map.put("msg","");
        return map;
    }

    //关闭帖子评论
    @RequestMapping("closeTopicById")
    @ResponseBody
    public Map closeTopicById(Topic topic){
        HashMap<String,Object> map = new HashMap<>();

        int result = topicService.closeTopicById(topic);

        map.put("result",result);
        map.put("code",200);
        map.put("msg","");
        return map;
    }

    //打开帖子评论权限
    @RequestMapping("reopenTopicById")
    @ResponseBody
    public Map reopenTopicById(Topic topic){
        HashMap<String,Object> map = new HashMap<>();
        int result = topicService.reopenTopicById(topic);

        map.put("result",result);

        map.put("code",200);
        map.put("msg","");
        return map;
    }

    //举报帖子
    @RequestMapping("informTopic")
    @ResponseBody
    public Map informTopic(Report report, String topic_t_id){
        HashMap<String,Object> map = new HashMap<>();

        int topic_id=Integer.parseInt(topic_t_id);
        report.setT_id(topic_id);
        int result = topicService.informTopic(report);


        map.put("code",200);
        map.put("msg","");
        return map;
    }

    //检查该用户是否收藏该贴
    @RequestMapping("checkCollectTopic")
    @ResponseBody
    public Map checkCollectTopic(HttpServletRequest request){
        HashMap<String,Object> map = new HashMap<>();
        String fid = request.getParameter("fid");
        CollectionTopic collectionTopic = new CollectionTopic();
        collectionTopic.setT_id(fid);
        System.out.println("checkCollect  Collection : " + request.getSession().getAttribute("u_id"));
        collectionTopic.setU_id(request.getSession().getAttribute("u_id").toString());

        int result = topicService.checkCollectTopic(collectionTopic);
        System.out.println("setServic : " + fid);

        map.put("code",200);
        map.put("msg","");
        map.put("data",result);     // 1是已收藏 0是未收藏
        return map;
    }


    //收藏帖子
    @RequestMapping("collectTopic")
    @ResponseBody
    public Map collectTopic(HttpServletRequest request){
        HashMap<String,Object> map = new HashMap<>();
        CollectionTopic collectionTopic = new CollectionTopic();
        String fid = request.getParameter("fid");
        collectionTopic.setU_id(request.getSession().getAttribute("u_id").toString());
        collectionTopic.setT_id(fid);
        System.out.println(request.getSession().getAttribute("u_id") + " start Add  Collection : " + fid);
        int result = topicService.collectTopic(collectionTopic);
        System.out.println(request.getSession().getAttribute("u_id") + " Add  Collection : " + fid);

        map.put("code",200);
        map.put("msg","收藏成功");
        return map;
    }


    //取消收藏
    @RequestMapping("deleteCollectionTopic")
    @ResponseBody
    public Map deleteCollectionTopic(HttpServletRequest request){
        HashMap<String,Object> map = new HashMap<>();
        CollectionTopic collectionTopic = new CollectionTopic();
        String fid = request.getParameter("fid");
        collectionTopic.setU_id(request.getSession().getAttribute("u_id").toString());
        collectionTopic.setT_id(fid);
        System.out.println(request.getSession().getAttribute("u_id") + " start Add  Collection : " + fid);

        int result = topicService.deleteCollectionTopic(collectionTopic);

        map.put("code",200);
        map.put("msg","删除成功");
        return map;
    }


    @RequestMapping("getTopicByID")
    @ResponseBody
    public Map getTopicByID(int t_id){
        HashMap<String,Object> map = new HashMap<>();
        List<Topic> TopicList = topicService.getTopicByID(t_id);
        if(TopicList.size() > 0) {
            map.put("code", 200);
            map.put("Topic", TopicList);
        }
        else{
            map.put("code", -1);
        }
        return map;
    }

}

